﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Curriculum_design
{
	class Snake:Draw
    {
		//蛇身
		private Label _body;
		//蛇的颜色
		private Color _color = Color.SkyBlue;
		//蛇的大小
		private Size _size = new Size(10,10);
		//移动方向默认向西
		private Curriculum_design.Way way = Way.WEST;
		//移动速度
		private int speed = 100;
		//所吃食物的个数
		private int foodCount = 0;
		//蛇
		private ArrayList snake;

		//判断食物是否在蛇身体里
		private bool hasFood = false;
		//颜色属性
		public Color Color { get => _color; set => _color = value; }

		//方向属性
		public Way SnakeWay
		{
			set
			{
				this.way = value;
			}
			get
			{
				return way;
			}
		}
		//速度属性
        public int Speed { get => speed; set => speed = value; }
		//所吃食物
		public int FoodCount { get => foodCount; set => foodCount = value; }

        public void DrawSnake()
        {
			//设置大小
			_size = new Size(10, 10);
			//设置身体
			snake = new ArrayList();
			for (int i = 0; i < 5; i++)
			{
				_body = new Label();
				_body.BackColor = _color;
				_body.Size = _size;
				_body.BorderStyle = BorderStyle.FixedSingle;
				_body.Location = new Point(200 + i * 10, 150);
				snake.Add(_body);
			}
		}
		//返回蛇体
		public ArrayList GetSnake()
		{
			return snake;
		}
		//蛇体移动
		public void Move(Control control)
		{
			if (!this.hasFood)
			{
				//Controls.Remove 可以实现一个一个的删除控件
				control.Controls.Remove(control.GetChildAtPoint(((Label)snake[snake.Count - 1]).Location));
				//删掉末尾的身体
				snake.RemoveAt(snake.Count - 1);
			}
			Label temp = new Label();
			this.CopyBody(temp, (Label)snake[0]);
			switch (this.way)
			{
				case Way.WEST:
					{
						temp.Left -= 10;
						snake.Insert(0, temp);
						break;
					}

				case Way.EAST:
					{
						temp.Left += 10;
						snake.Insert(0, temp);
						break;
					}
				case Way.NORTH:
					{
						temp.Top -= 10;
						snake.Insert(0, temp);
						break;
					}
				case Way.SOUTH:
					{
						temp.Top += 10;
						snake.Insert(0, temp);
						break;
					}
			}
			//添加身体
			control.Controls.Add((Label)snake[0]);
			if (this.hasFood)
			{
				this.hasFood = false;
			}

		}
		//copy蛇身
		//吃东西
		public bool Eat(Point food)
		{
			if (((Label)snake[0]).Left == food.X && ((Label)snake[0]).Top == food.Y)
			{
				//吃到东西
				this.hasFood = true;
				return true;
			}
			return false;
		}

        void CopyBody(Label x, Label y)
		{
			x.Location = y.Location;
			x.BackColor = y.BackColor;
			x.Size = y.Size;
			x.BorderStyle = y.BorderStyle;
		}
	}
}
